package com.atguigu.flink.chapter11;

import com.atguigu.flink.bean.WaterSensor;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

import java.time.Duration;

import static org.apache.flink.table.api.Expressions.$;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2021/10/20 15:17
 */
public class Flink10_Window_Grouped_1 {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
    
        DataStream<WaterSensor> stream =
            env
                .fromElements(new WaterSensor("sensor_1", 1000L, 10),
                              new WaterSensor("sensor_1", 4000L, 20),
                              new WaterSensor("sensor_1", 4001L, 40),
                              new WaterSensor("sensor_1", 5000L, 50),
                              new WaterSensor("sensor_2", 3000L, 30),
                              new WaterSensor("sensor_2", 6000L, 60))
                .assignTimestampsAndWatermarks(
                    WatermarkStrategy
                        .<WaterSensor>forBoundedOutOfOrderness(Duration.ofSeconds(3))
                        .withTimestampAssigner((ws, ts) -> ws.getTs())
                );
        
        StreamTableEnvironment tenv = StreamTableEnvironment.create(env);
    
        // 每隔2分钟统计最近2分钟内的每个传感器的水位和
        // select id, w, sum(vc) from s group by id,w
    
        Table table = tenv.fromDataStream(stream, $("id"), $("ts").rowtime(), $("vc"));
    
        tenv.createTemporaryView("sensor", table);
      
        // 滚动窗口
        /*tenv.sqlQuery("select " +
                          " id," +
                          " tumble_start(ts, interval '3' second) stt, " +
                          " tumble_end(ts, interval '3' second) edt, " +
                          " sum(vc) " +
                          "from sensor " +
                          "group by id, TUMBLE(ts, interval '3' second)").execute().print();*/
    
        /*tenv.sqlQuery("select " +
                          " id," +
                          " hop_start(ts, interval '3' second, interval '5' second) stt, " +
                          " hop_end(ts, interval '3' second, interval '5' second) edt, " +
                          " sum(vc) " +
                          "from sensor " +
                          "group by id, HOP(ts, interval '3' second, interval '5' second)").execute().print();*/
    
        tenv.sqlQuery("select " +
                          " id," +
                          " session_start(ts, interval '2' second) stt, " +
                          " session_end(ts, interval '2' second) edt, " +
                          " sum(vc) " +
                          "from sensor " +
                          "group by id, SESSION(ts, interval '2' second)").execute().print();
    
    }
}
